﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace Aliyun.MNS.Runtime.Internal.Util
{
    public class LogMessage
    {
        public object[] Args { get; private set; }
        public IFormatProvider Provider { get; private set; }
        public string Format { get; private set; }

        public LogMessage(string message) : this(CultureInfo.InvariantCulture, message) { }
        public LogMessage(string format, params object[] args) : this(CultureInfo.InvariantCulture, format, args) { }
        public LogMessage(IFormatProvider provider, string format, params object[] args)
        {
            Args = args;
            Format = format;
            Provider = provider;
        }

        public override string ToString()
        {
            return string.Format(Provider, Format, Args);
        }
    }

    public class Logger : ILogger
    {
        private static IDictionary<Type, Logger> cachedLoggers = new Dictionary<Type, Logger>();
        private InternalLogger internalLogger;
        private static Logger emptyLogger = new Logger();

        public static bool IsEnabled
        {
            get; set;
        }

        private Logger()
        {
            internalLogger = null;
        }
        private Logger(Type type)
        {
            internalLogger = new InternalLog4NetLogger(type);
        }

        #region Static accessor

        public static Logger GetLogger(Type type)
        {
            if (type == null) throw new ArgumentNullException("type");
            if (!IsEnabled)
            {
                return EmptyLogger;
            }

            Logger l;
            lock (cachedLoggers)
            {
                if (!cachedLoggers.TryGetValue(type, out l))
                {
                    l = new Logger(type);
                    cachedLoggers[type] = l;
                }
            }
            return l;
        }

        public static void ClearLoggerCache()
        {
            lock (cachedLoggers)
            {
                cachedLoggers = new Dictionary<Type, Logger>();
            }
        }

        public static Logger EmptyLogger { get { return emptyLogger; } }

        #endregion

        #region Logging methods

        public void Flush()
        {
            if (!IsEnabled)
            {
                return;
            }
            if (internalLogger != null)
            {
                internalLogger.Flush();
            }
        }

        public void Error(Exception exception, string message)
        {
            if (!IsEnabled)
            {
                return;
            }
            if (internalLogger != null)
            {
                internalLogger.Error(exception, message);
            }
        }

        public void Debug(Exception exception, string message)
        {
            if (!IsEnabled)
            {
                return;
            }
            if (internalLogger != null)
            {
                internalLogger.Debug(exception, message);
            }
        }

        public void Debug(string message)
        {
            if (!IsEnabled)
            {
                return;
            }
            if (internalLogger != null)
            {
                internalLogger.Debug(message);
            }
        }

        public void Info(string message)
        {
            if (!IsEnabled)
            {
                return;
            }
            if (internalLogger != null)
            {
                internalLogger.Info(message);
            }
        }

        #endregion

    }

    internal abstract class InternalLogger
    {
        public Type DeclaringType { get; private set; }

        public bool IsEnabled { get; set; }

        public InternalLogger(Type declaringType)
        {
            DeclaringType = declaringType;
            IsEnabled = true;
        }

        #region Logging methods

        /// <summary>
        /// Flushes the logger contents.
        /// </summary>
        public abstract void Flush();

        /// <summary>
        /// Simple wrapper around the log4net IsErrorEnabled property.
        /// </summary>
        public virtual bool IsErrorEnabled { get { return true; } }

        /// <summary>
        /// Simple wrapper around the log4net IsDebugEnabled property.
        /// </summary>
        public virtual bool IsDebugEnabled { get { return true; } }

        /// <summary>
        /// Simple wrapper around the log4net IsInfoEnabled property.
        /// </summary>
        public virtual bool IsInfoEnabled { get { return true; } }

        /// <summary>
        /// Simple wrapper around the log4net Error method.
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="messageFormat"></param>
        /// <param name="args"></param>
        public abstract void Error(Exception exception, string message);

        /// <summary>
        /// Simple wrapper around the log4net Debug method.
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="messageFormat"></param>
        /// <param name="args"></param>
        public abstract void Debug(Exception exception, string message);

        /// <summary>
        /// Simple wrapper around the log4net DebugFormat method.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="arguments"></param>
        public abstract void Debug(string message);

        /// <summary>
        /// Simple wrapper around the log4net InfoFormat method.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="arguments"></param>
        public abstract void Info(string message);

        #endregion
    }

}
